package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysUserCollection;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserLikeRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserLikeMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserLike;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserLikeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户点赞业务层处理
 *
 * @author mars
 * @date 2024-03-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserLikeServiceImpl implements ISysUserLikeService {

    private final SysUserLikeMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserLike add(SysUserLikeRequest request) {
        SysUserLike entity = SysUserLike.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserLikeRequest request) {
        SysUserLike entity = SysUserLike.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserLike getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserLike> pageList(SysUserLikeRequest request) {
        Page<SysUserLike> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserLike> query = this.buildWrapper(request);
        IPage<SysUserLike> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserLike> list(SysUserLikeRequest request) {
        LambdaQueryWrapper<SysUserLike> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public void deleteByArticle(String id) {
        Long userId = ContextUserInfoThreadHolder.get().getId();
        baseMapper.delete(new QueryWrapper<SysUserLike>().eq("article_id", id).eq("user_id", userId));
    }

    private LambdaQueryWrapper<SysUserLike> buildWrapper(SysUserLikeRequest param) {
        LambdaQueryWrapper<SysUserLike> query = new LambdaQueryWrapper<>();
        return query;
    }

}
